#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPainter>
#include <QFont.h>
#include <QTimer>
#include <QKeyEvent>
int i=0;bool over=0;
int a[7]={880,880,880,880,880,880,880};
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow),ifrun(false)
{
    ui->setupUi(this);
    this->setGeometry(QRect(500,50,1300,1020));
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::paintEvent(QPaintEvent *event){
    QPainter painter(this);
    if(!ifrun)
    Initnumber();
    //画背景
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(30,30,840,960);
    painter.setPen(Qt::blue);
    for(int i=1;i<=6;i++){
        painter.drawLine(30,150,870,150);
        painter.drawLine(30,150+i*120,870,150+i*120);
        painter.drawLine(30+i*120,30,30+i*120,990);
    }
    //画方格
    QFont font1("Courier",24);
    painter.setFont(font1);
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawText(900,30,d1);
painter.drawText(900,60,d2);painter.drawText(900,90,d3)
        ;painter.drawText(900,120,d4);painter.drawText(900,150,d5);

    painter.setPen(Qt::black);
    painter.setBrush(Qt::green);
    painter.drawRects(&number[0],number.size());
    if(over){
        timer->stop();
        timer2->stop();
        painter.drawText(900,200,d6);
    }
}
void MainWindow::Initnumber(){
    place=3;
    ifrun=true;
    d1="游戏规则：使用键盘";
    d2="“左”“右”“下”三个按键，";
    d3="控制数字的下落，";
    d4= "使相同的数字相遇";
    d5="得分：";
    d6="游戏结束";
    //QRect rect(390,30,120,120);
    //number=rect;
    number.resize(100);
    for(int p=0;p<number.size();p++){
        QRect rect(390,30,120,120);
        number[p]=rect;
    }
    timer=new QTimer(this);//设定计时器
    timer2=new QTimer(this);
    timer->start(1000);//时间1000ms
    timer2->start(100);//100ms
    connect(timer,SIGNAL(timeout()),SLOT(num_fall()));
    connect(timer2,SIGNAL(timeout()),SLOT(num_move()));
}void MainWindow::num_move(){
    if(number[i].bottom()>=a[place]){
        a[place]-=120;i++;place=3;
        update();}
   switch (direct) {
    case 1:
        if(place>0){
        number[i].setLeft(number[i].left()-120);
        number[i].setRight(number[i].right()-120);
        place--;}
        break;
    case 2:
        if(place<6){
        number[i].setLeft(number[i].left()+120);
        number[i].setRight(number[i].right()+120);
        place++;}
        break;
   case 3:
       if(number[i].bottom()<a[place]){
           number[i].setTop(number[i].top()+120);
           number[i].setBottom(number[i].bottom()+120);
       break;}
    default:
        break;
    }
    direct=4;
    update();

}
void MainWindow::num_fall(){

    if(number[i].bottom()>=a[place]){
        a[place]-=120;i++;place=3;if(a[place]<20)over=1;
        update();}
    number[i].setTop(number[i].top()+120);
    number[i].setBottom(number[i].bottom()+120);
    update();
}
void MainWindow::keyPressEvent(QKeyEvent *event){
    QKeyEvent *key=(QKeyEvent*) event;
    switch (key->key()){
    case Qt::Key_Left: direct=1;
        break;
    case Qt::Key_Right: direct=2;
        break;
    case Qt::Key_Down: direct=3;
    default:
        break;
    }
}
